<template>
  <div class="my-view">
    <h1 class="view-title">我的</h1>
    
    <div v-if="userInfo" class="user-info-container">
      <div class="user-header">
        <div class="avatar-container">
          <img :src="userInfo.avatar || defaultAvatar" alt="用户头像" class="user-avatar">
        </div>
        <div class="user-details">
          <h2 class="username">{{ userInfo.nickname || userInfo.username }}</h2>
          <p class="user-id">ID: {{ userInfo.id }}</p>
          <div class="user-actions">
            <el-button type="primary" size="small" @click="editProfile">编辑资料</el-button>
            <el-button type="danger" size="small" @click="handleLogout">退出登录</el-button>
          </div>
        </div>
      </div>
      
      <el-divider />
      
      <div class="user-stats">
        <div class="stat-item">
          <div class="stat-value">{{ userInfo.favorited_works?.length || 0 }}</div>
          <div class="stat-label">收藏作品</div>
        </div>
        <div class="stat-item">
          <div class="stat-value">0</div>
          <div class="stat-label">我的作品</div>
        </div>
        <div class="stat-item">
          <div class="stat-value">0</div>
          <div class="stat-label">我的模板</div>
        </div>
      </div>
      
      <el-divider />
      
      <div class="user-menu">
        <div class="menu-section">
          <h3 class="section-title">账号设置</h3>
          <div class="menu-items">
            <div class="menu-item" @click="goToSettings('profile')">
              <el-icon><User /></el-icon>
              <span>个人资料</span>
              <el-icon class="arrow-icon"><ArrowRight /></el-icon>
            </div>
            <div class="menu-item" @click="goToSettings('security')">
              <el-icon><Lock /></el-icon>
              <span>账号安全</span>
              <el-icon class="arrow-icon"><ArrowRight /></el-icon>
            </div>
            <div class="menu-item" @click="goToSettings('notification')">
              <el-icon><Bell /></el-icon>
              <span>消息通知</span>
              <el-icon class="arrow-icon"><ArrowRight /></el-icon>
            </div>
          </div>
        </div>
        
        <div class="menu-section">
          <h3 class="section-title">其他</h3>
          <div class="menu-items">
            <div class="menu-item" @click="goToSettings('about')">
              <el-icon><InfoFilled /></el-icon>
              <span>关于我们</span>
              <el-icon class="arrow-icon"><ArrowRight /></el-icon>
            </div>
            <div class="menu-item" @click="goToSettings('feedback')">
              <el-icon><ChatDotRound /></el-icon>
              <span>意见反馈</span>
              <el-icon class="arrow-icon"><ArrowRight /></el-icon>
            </div>
          </div>
        </div>
      </div>
    </div>
    
    <div v-else class="login-container">
      <el-empty description="您尚未登录">
        <el-button type="primary" @click="goToLogin">立即登录</el-button>
      </el-empty>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, computed } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage, ElMessageBox } from 'element-plus';
import { User, Lock, Bell, InfoFilled, ChatDotRound, ArrowRight } from '@element-plus/icons-vue';
import { useUserStore } from '@/stores/user';

const router = useRouter();
const userStore = useUserStore();
const defaultAvatar = 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png';

// 计算属性：用户信息
const userInfo = computed(() => userStore.user);

// 编辑个人资料
const editProfile = () => {
  router.push('/profile');
};

// 退出登录
const handleLogout = () => {
  ElMessageBox.confirm('确定要退出登录吗?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    userStore.logout();
    ElMessage.success('已退出登录');
    router.push('/login');
  }).catch(() => {
    // 用户取消退出
  });
};

// 前往登录页
const goToLogin = () => {
  router.push('/login');
};

// 前往设置页
const goToSettings = (section: string) => {
  if (section === 'profile' || section === 'security') {
    router.push({ path: '/profile', query: { tab: section } });
  } else if (section === 'notification') {
    router.push('/notifications');
  }
};

// 组件挂载时获取用户信息
onMounted(async () => {
  if (userStore.token) {
    await userStore.fetchUserInfo();
  }
});
</script>

<style lang="scss" scoped>
.my-view {
  padding: 20px;
}

.view-title {
  font-size: 24px;
  font-weight: 600;
  margin-bottom: 24px;
  color: #333;
}

.user-info-container {
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
  padding: 20px;
}

.user-header {
  display: flex;
  align-items: center;
  padding: 10px 0;
}

.avatar-container {
  margin-right: 20px;
}

.user-avatar {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  object-fit: cover;
  border: 2px solid #f0f0f0;
}

.user-details {
  flex: 1;
}

.username {
  font-size: 20px;
  font-weight: 600;
  margin: 0 0 5px 0;
}

.user-id {
  color: #909399;
  font-size: 14px;
  margin: 0 0 10px 0;
}

.user-actions {
  display: flex;
  gap: 10px;
}

.user-stats {
  display: flex;
  justify-content: space-around;
  padding: 15px 0;
}

.stat-item {
  text-align: center;
}

.stat-value {
  font-size: 24px;
  font-weight: 600;
  color: #409EFF;
}

.stat-label {
  font-size: 14px;
  color: #606266;
  margin-top: 5px;
}

.menu-section {
  margin-bottom: 20px;
}

.section-title {
  font-size: 16px;
  font-weight: 600;
  margin: 10px 0;
  color: #303133;
}

.menu-items {
  background-color: #f5f7fa;
  border-radius: 8px;
}

.menu-item {
  display: flex;
  align-items: center;
  padding: 15px;
  cursor: pointer;
  transition: background-color 0.3s;
  border-bottom: 1px solid #ebeef5;
}

.menu-item:last-child {
  border-bottom: none;
}

.menu-item:hover {
  background-color: #ecf5ff;
}

.menu-item .el-icon {
  margin-right: 10px;
  font-size: 18px;
  color: #409EFF;
}

.menu-item span {
  flex: 1;
  font-size: 14px;
}

.arrow-icon {
  color: #c0c4cc;
  margin-right: 0;
}

.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 400px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
}

@media (max-width: 768px) {
  .user-header {
    flex-direction: column;
    text-align: center;
  }
  
  .avatar-container {
    margin-right: 0;
    margin-bottom: 15px;
  }
  
  .user-actions {
    justify-content: center;
  }
}
</style> 